;
layui.define(['jquery', 'table', 'form', 'layer', 'tree'],
function(exports) {
	var $ = layui.$,
	form = layui.form,
	tree = layui.tree,
	layer = layui.layer,
	table = layui.table;
	var cols = [[{
		type: 'numbers',
		fixed: 'center',
		title: "序号",
		width: 50
	},
	{
		type: 'checkbox',
		fixed: 'left'
	},
	{
		field: 'roleName',
		title: '角色名称',
		minWidth: 200
	},
	{
		field: 'available',
		title: '状态',
		minWidth: 110,
		unresize: true,
		templet: function(res) {
			if (res.available == 1) {
				return '<input type="checkbox" name="role_lock" value="' + res.id + '" title="锁定" lay-filter="roleLock">';
			} else {
				return '<input type="checkbox" name="role_lock" value="' + res.id + '" title="锁定" lay-filter="roleLock" checked >';
			}
		}
	},
	{
		field: 'gmtCreate',
		title: '创建时间',
		width: 170
	},
	{
		field: 'remark',
		title: '描述',
		width: 500
	},
	{
		fixed: 'right',
		title: "操作",
		width: 170,
		align: 'center',
		templet: function(res) {
			var result = "";
			if(res.available == 1){
                if (layui.data('auth').authList.includes("role:edit")) {
                    result += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
                }
                if (layui.data('auth').authList.includes("role:auth")) {
                    result += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="auth"><i class="layui-icon layui-icon-auz"></i>授权</a>';
                }
			}
			return result;
		}
	}]];
	//权限判断
	if (!layui.data('auth').authList.includes("role:edit") && !layui.data('auth').authList.includes("role:auth")) {
		cols[0].pop();
	}
    //表格渲染
	table.render({
		elem: '#role-table',
		url: '/admin/role/inquiry/page',
		method: 'post',
		page: {
			layout: ['limit', 'count', 'prev', 'page', 'next', 'refresh', 'skip'],
			groups: 5 //只显示 1 个连续页码
		},
		limit: 30,
		limits: [30, 60, 90, 120, 150, 180],
		autoSort: false,
		//开启分页
		toolbar: '#role-toolbar',
		defaultToolbar: ['filter'],
		title: '角色数据表',
		height: 'full-130',
		loading: true,
		cols: cols,
		done: function(res, curr, count) {
			//这里可以用来设置disabled
			if (!layui.data('auth').authList.includes('role:status')) {
			    $("input[name='role_lock']").attr("disabled",true);
			}
		}
	});
	table.on('toolbar(role-table)',
	function(obj) {
		switch (obj.event) {
		case 'add':
			layer.open({
				type:
				2,
				title: '添加角色信息',
				content: '/role/add/html',
				maxmin: true,
				area: ['450px', '300px'],
				btn: ['确定', '取消'],
				yes: function(index, layero) {
					var iframeWindow = window['layui-layer-iframe' + index],
					submitID = 'role-add-submit',
					submit = layero.find('iframe').contents().find('#' + submitID);
					//监听提交
					iframeWindow.layui.form.on('submit(' + submitID + ')',
					function(data) {
						var userInfo = layui.data('admin').userInfo;
						$.extend(data.field, {
							'createOperator': userInfo.username
						});
						var jsonData = JSON.stringify(data.field);
						//提交 Ajax 成功后，静态更新表格中的数据
						$.ajax({
							url: '/admin/role/addition',
							type: 'post',
							contentType: "application/json",
							dataType: 'json',
							data: jsonData,
							success: function(data) {
								if (data.transactionStatus.success) {
									layer.msg("添加成功");
									table.reload('role-table'); //数据刷新
									layer.close(index); //关闭弹层
								} else {
									layer.msg(data.transactionStatus.replyText);
								}
							}
						});
					});
					submit.trigger('click');
				}
			});
			break;
		};
	});
	table.on('tool(role-table)',
	function(obj) {
		var data = obj.data;
		if (obj.event === 'del') {
			layer.confirm('真的删除行么',
			function(index) {
				layer.close(index);
			});
		} else if (obj.event === 'edit') {
			layer.open({
				type: 2,
				title: '修改用户信息',
				content: '/role/edit/html',
				maxmin: true,
				area: ['450px', '300px'],
				btn: ['确定', '取消'],
				yes: function(index, layero) {
					var iframeWindow = window['layui-layer-iframe' + index],
					submitID = 'role-edit-submit',
					submit = layero.find('iframe').contents().find('#' + submitID);
					//监听提交
					iframeWindow.layui.form.on('submit(' + submitID + ')',
					function(data) {
						var userInfo = layui.data('admin').userInfo;
						$.extend(data.field, {
							'modifyOperator': userInfo.username
						});
						var jsonData = JSON.stringify(data.field);
						//提交 Ajax 成功后，静态更新表格中的数据
						$.ajax({
							url: '/admin/role/modify',
							type: 'post',
							contentType: "application/json",
							dataType: 'json',
							data: jsonData,
							success: function(data) {
								if (data.transactionStatus.success) {
									layer.msg("修改成功");
									table.reload('role-table'); //数据刷新
									layer.close(index); //关闭弹层
								} else {
									layer.msg(data.transactionStatus.replyText);
								}
							}
						});
					});
					submit.trigger('click');
				},
				success: function(layero, index) {
					var jsonData = JSON.stringify({
						'id': data.id
					});
					$.ajax({
						url: '/admin/role/modify/inquiry',
						type: 'post',
						contentType: "application/json",
						dataType: 'json',
						data: jsonData,
						success: function(data) {
							if (data.transactionStatus.success) {
								var iframeWindow = window['layui-layer-iframe' + index];
								iframeWindow.initData(data);
							} else {
								layer.msg(data.transactionStatus.replyText);
							}
						}
					});
				}
			});
		} else if (obj.event === 'auth') {
			layer.open({
				type: 2,
				title: '授权',
				content: '/role/auth/html',
				maxmin: true,
				area: ['900px', '600px'],
				btn: ['确定', '取消'],
				yes: function(index, layero) {
					var iframeWindow = window['layui-layer-iframe' + index];
					iframeWindow.submitData(data.id);
					table.reload('role-table'); //数据刷新
					layer.close(index); //关闭弹层
				},
				success: function(layero, index) {
					var jsonData = JSON.stringify({
						'roleId': data.id
					});
					$.ajax({
						url: '/admin/role/auth/inquiry',
						type: 'post',
						contentType: "application/json",
						dataType: 'json',
						data: jsonData,
						success: function(data) {
							if (data.transactionStatus.success) {
								var iframeWindow = window['layui-layer-iframe' + index];
								iframeWindow.initData(data);
							} else {
								layer.msg(data.transactionStatus.replyText);
							}
						}
					});
				}
			});
		}
	});
	form.on('submit(role-search)',
	function(data) {
		var field = data.field;
		//执行重载
		table.reload('role-table', {
			where: field
		});
		return false;
	});
	form.on('checkbox(roleLock)',
	function(obj) {
		var id = this.value;
		var status;
		if (obj.elem.checked) {
			status = 0;
		} else {
			status = 1;
		}
		var userInfo = layui.data('admin').userInfo;
		var jsonData = JSON.stringify({
			"id": id,
			"available": status,
			'modifyOperator': userInfo.username
		});
		$.ajax({
			url: '/admin/role/modify/status',
			type: 'post',
			contentType: "application/json",
			dataType: 'json',
			data: jsonData,
			success: function(data) {
				if (data.transactionStatus.success) {
					layer.msg("修改成功");
					table.reload('role-table'); //数据刷新
				} else {
					layer.msg(data.transactionStatus.replyText);
				}
			}
		});
	});
	exports('roleList',
	function() {});
});